import pygame
import time
import random

pygame.init()
pygame.display.set_caption("贪吃蛇 good luck")
screen=pygame.display.set_mode([640,550])
screen.fill([255,255,255])

N=10
block_w=50
c_x=block_w//2
c_y=block_w//2
c_r=(block_w-6)//2

i=0
while i<N:
    j=0
    while j<N:
        pygame.draw.rect(screen,[0,255,0],[i*block_w,j*block_w,block_w,block_w],2)
        j+=1
    i+=1
b_1=random.randint(0,9)
b_2=random.randint(0,9)
a_1=b_1*50+25
a_2=b_2*50+25
pygame.draw.circle(screen, [0,255,0], [a_1,a_2], c_r)


# 存储一条蛇的坐标,每个元素是(x,y)元组,第0个是头
snake=[]

# color=[255,0,0]
def draw_snake(snake,color=[255,0,0]):
    is_clear=color==[255,255,255]
    direct="right"
    n0=snake[0]
    n1=snake[1]
    if n0[0]==n1[0]:
        if n0[1]>n1[1]:
            direct="down"
        else:
            direct="up"
    elif n0[0]>n1[0]:
        direct="right"
    else:
        direct="left"
    for i,node in enumerate(snake):
        if i==0:
            pygame.draw.circle(screen, color, node, c_r+2)
            if not is_clear:
                r=(c_r+2)//2
                points=[ (node[0],node[1]-r), (node[0],node[1]+r), (node[0]+2*r,node[1]) ]
                if direct=="left":
                    points=[ (node[0],node[1]-r), (node[0],node[1]+r), (node[0]-2*r,node[1]) ]
                elif direct=="up":
                    points=[ (node[0]-r,node[1]), (node[0]+r,node[1]), (node[0],node[1]-2*r) ]
                elif direct=="down":
                    points=[ (node[0]-r,node[1]), (node[0]+r,node[1]), (node[0],node[1]+2*r) ]
                pygame.draw.aalines(screen,[0,0,255],True,points)
        else:
            pygame.draw.circle(screen, color, node, c_r)

# 初始化这条蛇
snake_len=6  #初始长度为6
for i in range(snake_len):
    snake.append((c_x+(snake_len-1-i)*block_w,c_y))

draw_snake(snake)

c_x,c_y=snake[0] # x,y表示蛇头位置

pygame.display.flip()

def is_out_range(c_x,c_y):
    if c_x<=0 or c_x>=block_w*N:
        return True
    if c_y<=0 or c_y>=block_w*N:
        return True
    if is_snake_body(c_x, c_y):
        return True
    return False
def is_snake_body(c_x,c_y):
    shim=block_w//5
    for node in snake:
        if node[0]+shim>=c_x>=node[0]-shim and node[1]+shim>=c_y >= node[1]-shim:
            return True
    return False


running=True
while running:
    for event in pygame.event.get():
        # print(event)
        if event.type == pygame.QUIT:
            running=False
        elif event.type == pygame.KEYDOWN:
            c_x_old=c_x
            c_y_old=c_y
            # print(event.key)
            if event.key == pygame.K_UP:
                print("向上")
                if not is_out_range(c_x,c_y-1*block_w):
                    c_y-=block_w
            elif event.key == pygame.K_DOWN:
                print("向下")
                if not is_out_range(c_x,c_y+1*block_w):
                    c_y+=1*block_w
            elif event.key == pygame.K_LEFT:
                print("向左")
                if not is_out_range(c_x-1*block_w,c_y):
                    c_x-=1*block_w
            elif event.key == pygame.K_RIGHT:
                print("向右")
                if not is_out_range(c_x+1*block_w,c_y):
                    c_x+=1*block_w

            if c_x != c_x_old or c_y != c_y_old:
                #蛇头位置变动，修改整个蛇的每个节点的坐标
                draw_snake(snake,[255,255,255])
                snake.insert(0, (c_x,c_y))
                del snake[+1]
                draw_snake(snake)

                pygame.display.flip()
            if a_1==c_x and a_2==c_y:
                b_1=random.randint(0,9)
                b_2=random.randint(0,9)
                a_1=b_1*50+25
                a_2=b_2*50+25
                pygame.draw.circle(screen, [0,255,0], [a_1,a_2], c_r)



                   
pygame.quit()
b_1=random.randint(0,9)
b_2=random.randint(0,9)
a_1=b_1*50+25
a_2=b_2*50+25
pygame.draw.circle(screen, [0,255,0], [a_1,a_2], c_r)
if c_x != c_x_old or c_y != c_y_old:
                    draw_snake(snake,[255,255,255])
                    snake.insert(0, (c_x,c_y))
                    del snake[-1]
                    draw_snake(snake)